import { netMemberStep } from "./../service/TaskService";
import LogUtil from "../utils/LogUtil";
export const NameSpace = "MemberStepModel";

function createAction(action, payload) {
  return { type: `${NameSpace}/${action}`, payload };
}

function actionMember(memberStep) {
  return createAction("member", memberStep);
}

function actionCurrentChoose(choose) {
  return createAction("choose", choose);
}

export const MemberActions = {
  asyncActionMember() {
    return createAction("asyncMemberInfo", {});
  },
  asyncActionChoose(c) {
    return createAction("asyncSetCurrentChoose", c);
  }
};

export default {
  namespace: NameSpace,
  state: {
    memberStep: null,
    stat: 0,
    currentChoose: null
  },
  reducers: {
    member(state, { payload }) {
      return Object.assign({}, state, { memberStep: payload });
    },
    choose(state, { payload }) {
      return {
        ...state,
        currentChoose: payload
      };
    }
  },
  asyncTasks: {
    asyncMemberInfo({ dispatch }, { payload }) {
      netMemberStep(payload)
        .then(function(data) {
          dispatch(actionMember(data));
        })
        .catch(function(error) {
          LogUtil.err(error);
        });
    },
    asyncSetCurrentChoose({ dispatch }, { payload }) {
      dispatch(actionCurrentChoose(payload));
    }
  }
};
